import multiprocessing
import proc

class pynwheel:
    qmin = None
    qmout = None
    p = None
    fullscreen = False
    galaxyvis = None
    toolkit = None

    def hide_widgets(self, really=True):
        pass

    def eval_it(self, *args):
        if isinstance(self.p, multiprocessing.Process):
            if self.p.is_alive():
                self.p.terminate()
        if self.toolkit == "gtk":
            text = self.buff.get_text(self.buff.get_start_iter(), self.buff.get_end_iter())
        elif self.toolkit == "wx":
            text = self.code.GetValue()
        if text == "":
            return
        self.qmin = multiprocessing.Queue()
        self.qmout = multiprocessing.Queue()
        self.p = multiprocessing.Process(target=proc.proc_wrap, args=(self.qmin, self.qmout, text, self.galaxyvis.stars, self.galaxyvis.planets, self.galaxyvis.highlighted, self.galaxyvis.moons))
        self.p.start()

    def check_data(self):
        try:
            data = self.qmin.get(False)
        except:
            pass
        else:
            text = data["stdout"].getvalue()[:15000]
            if self.toolkit == "gtk":
                buff = self.disp.get_buffer()
                buff.set_text(text)
            elif self.toolkit == "wx":
                self.control.Clear()
                self.control.WriteText(text)
            if not isinstance(data["stars"], list):
                print "star_list has to be list"
            else:
                def starchk(e):
                    if not isinstance(e, dict):
                        return False
                    return True if ("x" in e and "y" in e and "z" in e and "sid" in e and "class" in e) else False
                if all(map(starchk, data["stars"])):
                    self.galaxyvis.stars = data["stars"]
                else:
                    print "star_list is broken, i wont use it"
            if not isinstance(data["planets"], list):
                print "planet_list has to be list"
            else:
                def planetchk(e):
                    if not isinstance(e, dict):
                        return False
                    return True if ("r" in e and "sid" in e and "distance" in e) else False
                if all(map(planetchk, data["planets"])):
                    self.galaxyvis.planets = data["planets"]
                else:
                    print "planet_list is broken, i wont use it"

            self.galaxyvis.highlighted = data["highlight"]
            self.galaxyvis.highlight(self.galaxyvis.highlighted)
            self.galaxyvis.create_lists(self.galaxyvis.startidx)
